
/**
 * Copyright 2017 Yonyou Corporation Ltd. All Rights Reserved.
 * This software is published under the terms of the Yonyou Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 *
 * @Project Name : dmscloud.manage
 * @File name : PermissionCtrlServiceImpl.java
 * @Author : yll
 * @Date : 2017年4月5日
 * <p>
 * ----------------------------------------------------------------------------------
 * Date       Who       Version     Comments
 * 1. 2017年4月5日    yll    1.0
 * <p>
 * <p>
 * <p>
 * <p>
 * ----------------------------------------------------------------------------------
 */

package com.yonyou.cyxdms.sysAuth.service.service.permission;

import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.CodeDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.PermissionCtrlPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.PermissionCtrlMapper;
import com.yonyou.cyxdms.sysAuth.service.service.manage.employee.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 受控权限实现类
 *
 * @author yll
 * @date 2017年4月5日
 */
@Service
public class PermissionCtrlServiceImpl implements PermissionCtrlService {

    @Autowired
    PermissionCtrlMapper permissionCtrlMapper;

    @Autowired
    private EmployeeService employeeService;

    /**
     * 添加一条受控权限
     *
     * @param permissionType
     * @param permissionid
     * @param ctrlCode
     * @param type
     * @throws ServiceBizException
     * @adu
     */
    @Override
    public Integer addPermissionCtrl(Integer permissionType, String permissionid, String ctrlCode, Integer type) throws ServiceBizException {
        if (StringUtils.isNullOrEmpty(permissionType)) {
            throw new ServiceBizException("权限类型不能为空");
        }
        if (StringUtils.isNullOrEmpty(permissionid)) {
            throw new ServiceBizException("角色id不能为空");
        }
        if (StringUtils.isNullOrEmpty(ctrlCode)) {
            throw new ServiceBizException("控制Code不能为空");
        }
        PermissionCtrlPO permissionctrlpo = new PermissionCtrlPO();
        permissionctrlpo.setPermissionId(Integer.parseInt(permissionid));
        permissionctrlpo.setPermissionType(permissionType);
        permissionctrlpo.setCtrlCode(ctrlCode);
        permissionctrlpo.setIsFlexible(ManageDictCodeConstants.STATUS_IS_NOT);
        permissionctrlpo.setIsEmpowered(ManageDictCodeConstants.STATUS_IS_YES);
        permissionctrlpo.insert();
        return permissionctrlpo.getPermissionCtrlId();
    }

    /**
     * 修改一条受控权限
     *
     * @param permissionType
     * @param permissionID
     * @param ctrlCode
     * @param type
     * @throws ServiceBizException
     * @author adu
     * @date 2019年8月12日
     */
    @Override
    public void editPermissionCtrl(Integer permissionType, String permissionID, String ctrlCode, Integer type) throws ServiceBizException {
        if (StringUtils.isNullOrEmpty(permissionID)) {
            throw new ServiceBizException("角色id不能为空");
        }
        if (StringUtils.isNullOrEmpty(ctrlCode)) {
            throw new ServiceBizException("控制代码不能为空");
        }
        PermissionCtrlPO po = new PermissionCtrlPO();
        // po.update("IS_EMPOWERED=?","PERMISSION_ID=? and CTRL_CODE=? and PERMISSION_TYPE=?", ManageDictCodeConstants.STATUS_IS_YES, PermissionID,ctrlCode,PermissionType);
    }

    @Override
    public void deleteMenuByPermissionId(Integer permissionType, String permissionID) throws ServiceBizException {
        StringBuilder sqlSb = new StringBuilder("select PERMISSION_CTRL_ID,DEALER_CODE,APP_ID  from tm_Permission_CTRL where PERMISSION_ID=? and PERMISSION_TYPE=?");
        List<Object> params = new ArrayList<>();
        params.add(Long.parseLong(permissionID));
        params.add(permissionType);
//        List<Map> list= DAOUtil.findAll(sqlSb.toString(),params);
//        for(int i=0;i<list.size();i++){
//            Object ctrlId=list.get(i).get("PERMISSION_CTRL_ID");
//            PermissionCtrlPO aa=PermissionCtrlPO.findById(ctrlId);
//            aa.delete();
//        }
    }

    /**
     * 加载所有受控权限
     *
     * @param PermissionType
     * @param id
     * @return
     * @throws ServiceBizException (non-Javadoc)
     * @author yll
     * @date 2017年4月5日
     * @see com.yonyou.dmscloud.manage.service.permission.PermissionCtrlService#queryMenuCtrl(Integer, String)
     */
    @Override
    public Map<String, String> queryMenuCtrl(Integer permissionType, String id) throws ServiceBizException {
        String maintain = null;//维修
        String accessories = null;//配件
        String vehicleWarehouse = null;//整车仓库
        String accessoriesWarehouse = null;//配件仓库
        String suppliesWarehouse = null;//用品仓库
        String favorableModels = null;//优惠模式
//        StringBuilder sqlSb = new StringBuilder("select PERMISSION_CTRL_ID,PERMISSION_ID,CTRL_CODE,TYPE from tm_permission_ctrl where 1=1");
        //去重

        List<String> idList = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(id)) {
            String[] ids = id.split(",");
            for (String splId : ids) {
                idList.add(splId);
            }
        }
        List<Map<String, Object>> list = permissionCtrlMapper.queryMenuCtrl(permissionType, idList, ManageDictCodeConstants.STATUS_IS_YES);

        Map<String, String> basicresult = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            String ctrlCode = (String) list.get(i).get("CTRL_CODE");
            Integer type = (Integer) list.get(i).get("TYPE");
            if (type.equals(ManageDictCodeConstants.MAINTAIN)) {
                if (maintain == null) {
                    maintain = ctrlCode;
                } else {
                    maintain += ",";
                    maintain += ctrlCode;
                }
            }
            if (type.equals(ManageDictCodeConstants.ACCESSORIES)) {

                if (accessories == null) {
                    accessories = ctrlCode;
                } else {
                    accessories += ",";
                    accessories += ctrlCode;
                }
            }
            if (type.equals(ManageDictCodeConstants.VEHICLE_WAREHOUSE)) {
                if (vehicleWarehouse == null) {
                    vehicleWarehouse = ctrlCode;
                } else {
                    vehicleWarehouse += ",";
                    vehicleWarehouse += ctrlCode;
                }
            }
            if (type.equals(ManageDictCodeConstants.ACCESSORIES_WAREHOUSE)) {
                if (accessoriesWarehouse == null) {
                    accessoriesWarehouse = ctrlCode;
                } else {
                    accessoriesWarehouse += ",";
                    accessoriesWarehouse += ctrlCode;
                }
            }
            if (type.equals(ManageDictCodeConstants.SUPPLIES_WAREHOUSE)) {
                if (suppliesWarehouse == null) {
                    suppliesWarehouse = ctrlCode;
                } else {
                    suppliesWarehouse += ",";
                    suppliesWarehouse += ctrlCode;
                }
            }
            if (type.equals(ManageDictCodeConstants.FAVORABLE_MODELS)) {
                if (favorableModels == null) {
                    favorableModels = ctrlCode;
                } else {
                    favorableModels += ",";
                    favorableModels += ctrlCode;
                }
            }

        }
        basicresult.put("maintain", maintain);
        basicresult.put("accessories", accessories);
        basicresult.put("vehicleWarehouse", vehicleWarehouse);
        basicresult.put("accessoriesWarehouse", accessoriesWarehouse);
        basicresult.put("suppliesWarehouse", suppliesWarehouse);
        basicresult.put("favorableModels", favorableModels);
        return basicresult;
    }

    /**
     * adu   2019-9-21
     *
     * @param permissionType
     * @param id
     * @return
     */
    @Override
    public List<CodeDTO> maintainRemoteUrl(Integer permissionType, String id) {
        return employeeService.findTcCodeByType(Long.valueOf(1211));
    }

    @Override
    public List<CodeDTO> accessoriesRemoteUrl(Integer permissionType, String id) {
        return employeeService.findTcCodeByType(Long.valueOf(1313));
    }

    @Override
    public void deleteMenuByPermissionId(Integer permissionType, String permissionID, Integer type) throws ServiceBizException {

    }


}
